<?php
declare (strict_types=1);

namespace app\services\statistics;


use app\dao\statistics\StaticStoreProductDao;
use app\services\BaseServices;
use app\model\product\product\StoreProduct;

class StaticStoreProductServices extends BaseServices
{
    /**
     * StaticStoreProductDao constructor.
     * @param StaticStoreProductDao $dao
     */
    public function __construct(StaticStoreProductDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @param $param
     */
    public function adds_static($param){
        foreach ($param['list'] as $key => $val){
            $data = [
                'uid' => $param['uid'] ?? 0,
                'product_id' => $val['id'] ?? 0,
                'product_name' => $val['store_name'] ?? '',
                'number' => $val['number'] ?? 1,
                'type' => $param['type'] ?? 0,
                'static_date' => date('Y-m-d'),
                'add_time' => time(),
            ];
            $this->dao->save($data);
        }
    }

    /**
     * @param $param
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add_static($param){
        $info = StoreProduct::where('id', $param['product_id'])->find();
        $param['product_name'] = $info['store_name'];

        $data = [
            'uid' => $param['uid'] ?? 0,
            'product_id' => $param['product_id'] ?? 0,
            'product_name' => $param['product_name'],
            'number' => $param['number'] ?? 1,
            'sku' => $param['sku'] ?? '',
            'type' => $param['type'] ?? 0,
            'static_date' => date('Y-m-d'),
            'add_time' => time(),
        ];
        $this->dao->save($data);
    }
}